<?php
//
// XML网站地图生成插件
// 
// 
// Copyright 2018, 壹味科技
// http://www.evzg.com/
// 
// Author:TianQi,GaoYong
// Released on:2018-05-24
//
namespace plugins\evzg_sitemap\controller;

use cmf\controller\PluginAdminBaseController;
use app\portal\model\PortalCategoryModel;
use app\portal\model\PortalPostModel;
use think\Db;

class AdminIndexController extends PluginAdminBaseController
{

    protected function _initialize()
    {
        parent::_initialize();
        $adminId = cmf_get_current_admin_id();
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        }
    }

    public function index()
    {
        $request = $this->request;
        $host=$request->domain();
        $this->assign("host", $host);
        return $this->fetch('/admin_index');
    }

    public function addxml()
    {
        $site = $this->siteinfo();
        $html = '<a href="'.$site.'/">首页</a>';
        $lastmod=date('Y-m-d H:i:s',time());
        $xml = '<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xsl" href="'.$site.'/plugins/evzg_sitemap/evzg_sitemap.xsl"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0"><url><loc>'.$site.'/</loc><changefreq>daily</changefreq><priority>1</priority><lastmod>'.$lastmod.'</lastmod></url>';
       
       // 输出分类
        $portalCategoryModel = new PortalCategoryModel();
        $category = $portalCategoryModel->where('status', 1)->where('delete_time', 0)->select();
        foreach($category as $val)
        {
            $lastmod=date('Y-m-d H:i:s',time());
            $geturl=$site.cmf_url('portal/List/index','id='.$val['id']);
            $html .= '<a href="'.$geturl.'">'.$val['name'].'</a>'."<br>";
            $xml .= '<url><loc>'.$geturl.'</loc><changefreq>daily</changefreq><priority>0.8</priority><lastmod>'.$lastmod.'</lastmod></url>';
        }

        // 输出页面
        $PortalPostModel = new PortalPostModel();
        $where['post_status'] = '1';
        $where['post_type'] = '2';
        $article = $PortalPostModel->where($where)->where('delete_time', 0)->select();
        foreach($article as $value)
        {
            $lastmod=date('Y-m-d H:i:s',time());
            $geturl=$site.cmf_url('portal/Page/index','id='.$value['id']);
            $html .=  '<a href="'.$geturl.'">'.$value['post_title'].'</a>'."<br>";
            $xml .= '<url><loc>'.$geturl.'</loc><changefreq>monthly</changefreq><priority>0.5</priority><lastmod>'.$lastmod.'</lastmod></url>';
        }


        // 输出文章
        $where['post_status'] = '1';
        $where['post_type'] = '1';
        $article = $PortalPostModel->where($where)->where('delete_time', 0)->select();
        foreach($article as $value)
        {
        		$lastmod=date('Y-m-d H:i:s',$value['create_time']);

        		// 设定分类查询条件
        		$cidwhere['status'] = '1';
        		$cidwhere['post_id'] = $value['id'];

        		// 查询与文章匹配的分类
        		$cid = Db::name('portal_category_post')->where($cidwhere)->select();

        		// 假如一个文章有多个分类，在这里循环输出
        		foreach($cid as $v)
        		{
        			$geturl=$site.cmf_url('portal/Article/index',array('id'=>$value['id'],'cid'=>$v['category_id']));
        	    	$html .=  '<a href="'.$geturl.'">'.$value['post_title'].'</a>'."<br>";
            		$xml .= '<url><loc>'.$geturl.'</loc><changefreq>monthly</changefreq><priority>0.5</priority><lastmod>'.$lastmod.'</lastmod></url>';
        		}
       
        }
        $xml .= '</urlset>';
        $this->updatexml($xml);
        $this->success("生成成功！！");
    }


    private function siteinfo()
    {
        $request = $this->request;
        return $request->domain();
    }
    private function updatetxt($data)
    {
        $this->writefile('txt',$data);
    }
    private function updatexml($data)
    {
        $this->writefile('xml',$data);
    }
    private function writefile($type,$data)
    {
        $myfile = fopen($_SERVER['DOCUMENT_ROOT']."/sitemap.".$type, "w") or die("Unable to open file!");
        fwrite($myfile, $data);
        fclose($myfile);
    }

}
